import Vue from 'vue'
import VueRouter from 'vue-router'
import iView from 'view-design'
import { Message } from 'view-design'
import Routers from  '@/routers/router'
import App from '@/App.vue'
import 'view-design/dist/styles/iview.css'
import axios from 'axios'
import config from '@/config/config'
import store from '@/store'
import PersonInput from '@/common/components/person-input-res.vue'
import 'core-js/stable'
import 'regenerator-runtime/runtime'

// 组件
Vue.config.productionTip = false
Vue.use(iView)
Vue.use(VueRouter)
Vue.component('PersonInput', PersonInput)

// axios - 实例化
var axiosObj = axios.create({
    baseURL: config.apiHost,
    timeout: 5000
})
var axiosResObj = axios.create({
    baseURL: config.resHost,
    timeout: 5000
})
// axios - 拦截器
axiosObj.interceptors.response.use(
    function (response) {
        // 对响应数据做点什么
        return response
    }, function (error) {
        // 对响应错误做点什么
        if (error.response.status === 401) {
            if (window.location.hash !== '#/') {
                Message.error('身份信息失效，请重新登录！');
                router.push('/')
            }
        } else {
            Message.error('远程接口调用错误，请联系管理员！');
        }
        return Promise.reject(error)
    }
)

// 路由 - 实例化
const router = new VueRouter({
    routes: Routers // (缩写) 相当于 routes: routes
})
// 路由 - 导航首位
router.beforeEach((to, from, next) => {
    if (to.path == '/') {
        next()
    } else if (window.localStorage.getItem("token") == null) {
        Message.error('身份信息失效，请重新登录！');
        next({path: '/'})
    } else {
        next()
    }
})

// prototype
Vue.prototype.$ajax = axiosObj
Vue.prototype.$ajaxRes = axiosResObj
Date.prototype.Format = function (fmt) {
    const o = {
        "M+": this.getMonth() + 1, //月份
        "d+": this.getDate(), //日
        "H+": this.getHours(), //小时
        "m+": this.getMinutes(), //分
        "s+": this.getSeconds(), //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds() //毫秒
    }
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length))
    for (let k in o){
        if (new RegExp("(" + k + ")").test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)))
        }
    }
    return fmt
}

let v = new Vue({
    router: router,
    store: store,
    render: render => render(App)
}).$mount('#app')

export default v
